define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/user/feedback/index' + location.search,
                    edit_url: 'xiluhouse/user/feedback/edit',
                    del_url: 'xiluhouse/user/feedback/del',
                    multi_url: 'xiluhouse/user/feedback/multi',
                    user_url: 'xiluhouse/user/user/index',
                    house_url: 'xiluhouse/rent/rental/index',
                    table: 'feedback',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.user_url).data("primaryKey", "id").data("field", "mobile");
                $("input[name='house_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.house_url).data("primaryKey", "id").data("field", "title");
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        {
                            field: 'user_id', title: __('User_id'), formatter: function (value, row, index) {
                                if (row.user_id > 0) {
                                    var html = '';
                                    var url = $.fn.bootstrapTable.defaults.extend.user_url + "?id=" + row.user_id;
                                    html += '<a href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【' + row.user_id + '】' + row.user.nickname + '<br/>' + row.user.mobile + ' </a>';
                                    return html;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'house_id', title: __('House_id'), formatter: function (value, row, index) {
                                if (row.house_id > 0 && row.house) {
                                    var html = '';
                                    var url = $.fn.bootstrapTable.defaults.extend.house_url + "?id=" + row.house_id;
                                    html += '<a href="' + url + '" class="bg-info btn-dialog" title="查看房源信息">';
                                    html += '【' + row.house_id + '】' + row.house.name + '<br/>';
                                    html += '</a>';
                                    return html;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'content', title: __('Content'), operate: 'LIKE' },
                        { field: 'feedbacktype.name', title: __('Type_id'), operate: 'LIKE' },
                        { field: 'audit_state', title: __('Audit_state'), searchList: { "1": __('Audit_state 1'), "2": __('Audit_state 2') }, formatter: Table.api.formatter.normal },
                        { field: 'remark', title: __('Remark'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/user/feedback/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/user/feedback/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/user/feedback/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});